package com.megaspacevr.service.impl;

import com.megaspacevr.entity.PersonalInfo;
import com.megaspacevr.mapper.PersonalInfoMapper;
import com.megaspacevr.service.PersonalInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 个人信息表-用户填写的个人资料 服务实现类
 * </p>
 *
 * @author yezi
 * @since 2025-06-17
 */
@Service
public class PersonalInfoServiceImpl extends ServiceImpl<PersonalInfoMapper, PersonalInfo> implements PersonalInfoService {

    @Override
    @Transactional
    public PersonalInfo saveOrUpdatePersonalInfo(PersonalInfo personalInfo) {
        // 设置时间戳
        LocalDateTime now = LocalDateTime.now();
        if (personalInfo.getId() == null) {
            personalInfo.setCreatedAt(now);
        }
        personalInfo.setUpdatedAt(now);
        
        // 如果是新用户的第一个个人信息，设置为默认
        if (personalInfo.getId() == null) {
            PersonalInfo existingInfo = getPersonalInfoByUserToken(personalInfo.getUserToken());
            if (existingInfo == null) {
                personalInfo.setIsDefault(true);
            }
        }
        
        saveOrUpdate(personalInfo);
        
        // 如果设置为默认，需要将该用户的其他个人信息设置为非默认
        if (Boolean.TRUE.equals(personalInfo.getIsDefault())) {
            setDefaultPersonalInfo(personalInfo.getUserToken(), personalInfo.getId());
        }
        
        return personalInfo;
    }

    @Override
    public PersonalInfo getPersonalInfoByUserToken(String userToken) {
        return lambdaQuery()
                .eq(PersonalInfo::getUserToken, userToken)
                .eq(PersonalInfo::getIsDefault, true)
                .one();
    }

    @Override
    public boolean isPhoneExists(String phone, Long excludeId) {
        if (excludeId != null) {
            return lambdaQuery()
                    .eq(PersonalInfo::getPhone, phone)
                    .ne(PersonalInfo::getId, excludeId)
                    .count() > 0;
        } else {
            return lambdaQuery()
                    .eq(PersonalInfo::getPhone, phone)
                    .count() > 0;
        }
    }

    @Override
    @Transactional
    public void setDefaultPersonalInfo(String userToken, Long personalInfoId) {
        // 将该用户的所有个人信息设置为非默认
        LambdaUpdateWrapper<PersonalInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(PersonalInfo::getUserToken, userToken)
                     .set(PersonalInfo::getIsDefault, false);
        update(updateWrapper);
        
        // 将指定的个人信息设置为默认
        LambdaUpdateWrapper<PersonalInfo> defaultWrapper = new LambdaUpdateWrapper<>();
        defaultWrapper.eq(PersonalInfo::getId, personalInfoId)
                      .set(PersonalInfo::getIsDefault, true);
        update(defaultWrapper);
    }

    @Override
    public PersonalInfo getPersonalInfoByPhone(String phone) {
        return lambdaQuery()
                .eq(PersonalInfo::getPhone, phone)
                .one();
    }
}
